package xj.graph2d.app;

import java.io.IOException;

import xj.graph2d.DocumentConstants;
import xj.graph2d.DocumentException;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;

public class DrawAppDocument extends SimpleDrawDocument {

  public DrawAppDocument(DrawApp app) {
    super(app);
    this.app = app;
    this.filetype = DocumentConstants.XML_FILE_TYPE;
  }

  @Override
  public SimpleDrawDocument makeDocument() { 
    return new DrawAppDocument(app);
  }

  @Override
  public SimpleDrawView makeView() { 
    return new DrawAppView(app, this);
  }

  /*
   * public void newFile() { title = null; description = null;
   * resetUndoManager(); super.newFile(); }
   */

  @Override
  public void openFile(String filename, int filetype) 
    throws IOException, DocumentException {
    if (filename != null) {
      resetUndoManager();
      setChanged();
      shapes.clear();
      group = null;

      if (filetype != DocumentConstants.DEFAULT_FILE_TYPE && 
	  filetype != DocumentConstants.SERIALIZATION_FILE_TYPE && 
	  filetype != DocumentConstants.XML_FILE_TYPE) {
	int i = filename.lastIndexOf('.');
	if (i > 0 && i < filename.length() - 1) {
	  String fext = filename.substring(i + 1).toLowerCase();
	  filetype = DrawApp.getFiletype(fext);
	}
      }
      if (filetype == DocumentConstants.DEFAULT_FILE_TYPE) {
	if (!openXMLFile(filename)) {
	  // open XML file failed
	  System.out.println("Unable to load file: " + filename);
	}
      } else if (filetype == DocumentConstants.SERIALIZATION_FILE_TYPE) {
	try {
	  openSerializedFile(filename);
	} catch (IOException e1) {
	  System.out.println("Unable to open file: " + filename);
	  e1.printStackTrace();
	} catch (ClassNotFoundException e2) {
	  System.out.println(e2);
	  e2.printStackTrace();
	}
      } else if (filetype == DocumentConstants.XML_FILE_TYPE) {
	if (!openXMLFile(filename)) {
	  // open XML file failed
	  System.out.println("Unable to load file: " + filename);
	}
      }
    }
  }

  @Override
  public String getExtension(int filetype) {
    return DrawApp.getExtension(filetype);
  }

  @Override
  public boolean saveFile(String filename, int filetype, boolean checkExists)
      throws IOException, DocumentException {
    if (debug) {
      System.out.println("DrawAppDocument.saveFile()");
    }

    boolean result = false;
    if (filename != null) {
      String ext = getExtension(filetype);
      String fext = null;
      int i = filename.lastIndexOf('.');
      if (i > 0 && i < filename.length() - 1) {
	fext = filename.substring(i + 1).toLowerCase();
      }
      if (ext != null) {
	if (fext != null) {
	  if (!ext.equals(fext)) {
	    filename += ("." + ext);
	    app.setCurrentFilename(filename);
	  }
	} else {
	  filename += ("." + ext);
	  app.setCurrentFilename(filename);
	}
      } else if (fext != null) {
	filetype = DrawApp.getFiletype(fext);
      }
      setFilename(filename);

      if (filetype == DocumentConstants.DEFAULT_FILE_TYPE) {
	result = saveXMLFile(filename, checkExists);
      } else if (filetype == DocumentConstants.SERIALIZATION_FILE_TYPE) {
	result = saveSerializedFile(filename, checkExists);
      } else if (filetype == DocumentConstants.XML_FILE_TYPE) {
	result = saveXMLFile(filename, checkExists);
      } else {
	System.out.println("DrawAppDocument.saveFile() unknown filetype: " + filetype);
      }
      if (result) {
	documentSaved();
      }
    }
    return result;
  }

  protected DrawApp app;

  private static final boolean debug = false;

}
